// const _wx = require("../__antmove_wechat/api/index.js")(wx);
/**
 * 用户登录状态的相关封装
 */

import local from "/local";
import nav from "/nav";

const ACCESS_TOKEN_KEY = "ACCESS_TOKEN";
const REFRESH_TOKEN_KEY = "REFRESH_TOKEN";
const TENANT_ID_KEY = "TENANT_ID";
const PERMISSION_INFO_KEY = "PERMISSION_INFO";

/**
 * @description 保存不需要登录的页面路径地址
 * @param {数组}
 */
const noLogin = ["/login/login"];

/**
 * @description 判断访问的Page是否需要登录
 * 如果需要登录, 则判断是否已登录,未登录则跳转到登陆界面
 * @param {string} url 打开的页面
 */
const checkLogin = url => {
  let path = url;
  if (url.indexOf("?")) {
    path = url.split("?")[0];
  }
  // 保存不需要登录的页面,如果pageUrl不在数组中存在,则需要登录
  let needLogin = true;
  for (let item of noLogin) {
    if (item === path) needLogin = false;
  }
  // 如果需要登录,并且没有登录token标识,则跳转到登陆页面
  // let token = local.getLocal('token');
  // let token = local.getLocal(ACCESS_TOKEN_KEY);
  let token = local.getLocal('userID');
  if (needLogin && !token) {
    // 跳转登陆
    local.setLocal("NAV_URL", url);
    nav.navigateTo("/login/login");
  } else {
    return true;
  }
  return false;
};

/**
 * @description 更新用户信息
 * @param {object} data 用户信息
 */
const setToken = data => {
  local.setLocal(ACCESS_TOKEN_KEY, data || "");
  // local.setLocal(REFRESH_TOKEN_KEY, data && data.refreshToken || '')
  // local.setLocal(ACCESS_TOKEN_KEY, data && data.accessToken || '')
  // local.setLocal(REFRESH_TOKEN_KEY, data && data.refreshToken || '')
};

/**
 * @description 获取用户token
 */
const getAccessToken = () => {
  return local.getLocal(ACCESS_TOKEN_KEY);
};

/**
 * @description 获取刷新token
 */
const getRefreshToken = () => {
  return local.getLocal(REFRESH_TOKEN_KEY);
};

/**
 * @description 设置租户id
 */
const setTenantId = data => {
  local.setLocal(TENANT_ID_KEY, data || "");
};

/**
 * @description 获取租户id
 */
const getTenantId = () => {
  return local.getLocal(TENANT_ID_KEY);
};

/**
 * @description 设置用户权限信息
 */
const setPermissionInfo = data => {
  local.setLocal(PERMISSION_INFO_KEY, data || "");
};

/**
 * @description 获取用户权限信息
 */
const getPermissionInfo = () => {
  return local.getLocal(PERMISSION_INFO_KEY);
};

// export {
export default{
  checkLogin,
  setToken,
  getAccessToken,
  getRefreshToken,
  setTenantId,
  getTenantId,
  setPermissionInfo,
  getPermissionInfo
};
